<?php

namespace Inter\ArticleBundle\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Length;
use Inter\ArticleBundle\Model\Article as BaseArticle;


use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity(repositoryClass="Inter\ArticleBundle\Entity\Repository\ArticleRepository")
 * @ORM\Table(name="article")
 * @ORM\HasLifecycleCallbacks()
 */
class Article extends BaseArticle
{
	
    /**
     * @ORM\Column(type="string")
     */
    private $title;
    
    /**
     * @ORM\Column(type="string")
     */
    private $slug;

    /**
     * @ORM\Column(type="text")
     */
    private $description;

    /**
     * @ORM\Column(type="text")
     */
    private $content;

    /**
     * @ORM\Column(type="datetime")
     */
    private $createdat;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;    
    

   /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="article")
     * @ORM\JoinColumn(name="author", referencedColumnName="id")
     */
    protected $author;

    
    /**
     * @ORM\OneToMany(targetEntity="Comment", mappedBy="article")
     */
    protected $comments;
    

    public function __construct()
    {
    	$this->comments = new ArrayCollection();
    	$this->setCreatedat(new \DateTime());
    	//$this->setUpdateDate(new \DateTime());    	
    }
    
    
    

    /**
     * Set title
     *
     * @param string $title
     * @return Article
     */
    public function setTitle($title)
    {
        $this->title = $title;
        $this->setSlug($this->title);
        
        return $this;
    }

    /**
     * Get title
     *
     * @return string 
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Set description
     *
     * @param string $description
     * @return Article
     */
    public function setDescription($description)
    {
        $this->description = $description;

        return $this;
    }

    /**
     * Get description
     *
     * @return string 
     */
    public function getDescription()
    {
        return $this->description;
    }

    /**
     * Set content
     *
     * @param string $content
     * @return Article
     */
    public function setContent($content)
    {
        $this->content = $content;

        return $this;
    }

    /**
     * Get content
     *
     * @return string 
     */
    public function getContent($length=null)
    {   
        if (false === is_null($length) && $length > 0)
        	return substr($this->content, 0, $length);
        else
        	return $this->content;
    }

    /**
     * Set createdat
     *
     * @param \DateTime $createdat
     * @return Article
     */
    public function setCreatedat($createdat)
    {
        $this->createdat = $createdat;

        return $this;
    }

    /**
     * Get createdat
     *
     * @return \DateTime 
     */
    public function getCreatedat()
    {
        return $this->createdat;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set author
     *
     * @param \Inter\ArticleBundle\Entity\User $author
     * @return Article
     */
    public function setAuthor(User $author)
    {
        $this->author = $author;

        return $this;
    }

    /**
     * Get author
     *
     * @return \Inter\ArticleBundle\Entity\User 
     */
    public function getAuthor()
    {
        return $this->author;
    }
    
    /**
     * 
     * @param string $slug
     */
    public function setSlug($slug)
    {
    	$this->slug = $slug;
    	
    	return $this;
    } 
    
    /**
     *     
     */
    public function getSlug()
    {
    	return $this->slug;
    }
    
    /**
     * Slugify url
     */
    public function slugify($text)
    {
    	/* Replace none letter or digit by '-' */
    	$text = preg_replace('#[^\\pL\d]+#u','-',$text);
    	
    	/* trim */
    	$text = trim($text,'-');
    	
    	/* Transliterate */
    	if(function_exists('iconv'))
    	{
    		setlocale(LC_CTYPE, 'en_US.utf8');
    		//$text = iconv('utf-8','us-ascii//TRANSLIT',$text);
    		//$text = iconv('UTF-8', 'ASCII//TRANSLIT', $text);
    		$text = $this->utf8convert($text);
    		//echo $text;
    		//exit;
    	}
    	
    	/* lower case */
    	$text = strtolower($text);
    	
    	/* remove unwanted characters */
    	$text = preg_replace('#[^-\w]+#','',$text);
    	if(empty($text))
    	{
    		return 'n-a';
    	}
    	
    	return $text;
    }
    
    
    function utf8convert($str) {
    	
    	if(!$str) return false;
    	
    	$utf8 = array(
    			
    			'a'=>'á|à|ả|ã|ạ|ă|ắ|ặ|ằ|ẳ|ẵ|â|ấ|ầ|ẩ|ẫ|ậ|Á|À|Ả|Ã|Ạ|Ă|Ắ|Ặ|Ằ|Ẳ|Ẵ|Â|Ấ|Ầ|Ẩ|Ẫ|Ậ',
    			
    			'd'=>'đ|Đ',
    			
    			'e'=>'é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ|É|È|Ẻ|Ẽ|Ẹ|Ê|Ế|Ề|Ể|Ễ|Ệ',
    			
    			'i'=>'í|ì|ỉ|ĩ|ị|Í|Ì|Ỉ|Ĩ|Ị',
    			
    			'o'=>'ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ|Ó|Ò|Ỏ|Õ|Ọ|Ô|Ố|Ồ|Ổ|Ỗ|Ộ|Ơ|Ớ|Ờ|Ở|Ỡ|Ợ',
    			
    			'u'=>'ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự|Ú|Ù|Ủ|Ũ|Ụ|Ư|Ứ|Ừ|Ử|Ữ|Ự',
    			
    			'y'=>'ý|ỳ|ỷ|ỹ|ỵ|Ý|Ỳ|Ỷ|Ỹ|Ỵ',
    			
    	);
    	
    	foreach($utf8 as $ascii=>$uni) $str = preg_replace("/($uni)/i",$ascii,$str);
    	
    	return $str;
    	
    }
    
    /**
     * 
     * @param ClassMetadata $metadata
     */    
    
    public static function loadValidatorMetadata(ClassMetadata $metadata)
    {
    	
    	$metadata->addPropertyConstraint('title', new NotBlank());
    	$metadata->addPropertyConstraint('content', new NotBlank());
    	$metadata->addPropertyConstraint('description', new NotBlank());
    	 
    	$metadata->addPropertyConstraint('title', New Length(array('min'=>10)) );
    	$metadata->addPropertyConstraint('content', new Length(array('min'=>50)));
    	$metadata->addPropertyConstraint('description', new Length(array('min'=>50)));
    }
    
    
    
    

    /**
     * Add comments
     *
     * @param \Inter\ArticleBundle\Entity\Comment $comments
     * @return Article
     */
    public function addComment(\Inter\ArticleBundle\Entity\Comment $comments)
    {
        $this->comments[] = $comments;

        return $this;
    }

    /**
     * Remove comments
     *
     * @param \Inter\ArticleBundle\Entity\Comment $comments
     */
    public function removeComment(\Inter\ArticleBundle\Entity\Comment $comments)
    {
        $this->comments->removeElement($comments);
    }

    /**
     * Get comments
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getComments()
    {
        return $this->comments;
    }
}
